/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package orders

import (
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	//"github.com/mjiulee/lego/utils"
)

/* 根据id获取记录
 * -----------------
 */
func (t *ReceiveAddress) RecodeById(id int64) *ReceiveAddress {
	item := new(ReceiveAddress)
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil {
		return nil
	}
	return item
}

func (t *ReceiveAddress) RecodeByIdByWho(uid, id int64) *ReceiveAddress {
	item := new(ReceiveAddress)
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).And("uid = ?", uid).Get(item)
	if item.Id <= 0 || err != nil {
		return nil
	}
	return item
}

/* 添加
 * -----------------
 */
func (t *ReceiveAddress) AddRecode(item2add *ReceiveAddress) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ReceiveAddress) AddRecodeWithsession(session *xorm.Session, item2add *ReceiveAddress) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(硬删除)*/
func (t *ReceiveAddress) DelRecodeById(id int64) bool {
	item := new(ReceiveAddress)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ReceiveAddress) DelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(ReceiveAddress)
	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(软删除)*/
func (t *ReceiveAddress) SDelRecodeById(id int64) bool {
	item := new(ReceiveAddress)
	item.IfDel = 1
	_, err := lego.GetDBEngine().ID(id).Cols("if_del").Update(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ReceiveAddress) SDelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(ReceiveAddress)
	_, err := session.ID(id).Cols("if_del").Update(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 更新
 * -----------------*/
func (t *ReceiveAddress) UpdateRecode(rc *ReceiveAddress) bool {
	_, err := lego.GetDBEngine().ID(rc.Id).Omit("id", "if_del").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ReceiveAddress) UpdateRecodeFields(rc *ReceiveAddress, cols ...string) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Cols(cols...).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ReceiveAddress) UpdateRecodeWithSession(session *xorm.Session, rc *ReceiveAddress) bool {
	_, err := session.ID(rc.Id).Omit("id", "if_del").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/*查询表名, 注意，函数名不能定义未TableName()，因为会与xorm内部使用的TableName冲突*/
func (t *ReceiveAddress) TName() string {
	tname := lego.GetDBEngine().TableName(t)
	return tname
}
